<?php

//Load package classes
require_once('gcurl.class.php');

/**
 * eContetn api
 *
 */
class Econtent_API{

	/** confuguration
	* @var array
	*/
	var $cfg = array();

	/** url list for queries
	* @var obj
	*/
	var $gcurl;


	/**
	 * __construct
	 * This construct function.
	 *
	 * @version 2013-06-23
	 * @return object
	 */
	public function __construct(){
		$this->cfg = _cfg('econtent_api');
	}


	/**
	 * isACValidation
	 * The function checks the validity of access code.
	 *
	 * <code>
	 *  $eC_API = new Econtent_API();
	 *  $result = $eC_API->isACValidation("123456");
	 *  $isbn = $result->data->isbn;
	 * </code>
	 *
	 * @param string $access_code
	 * @version 2013-06-23
	 * @return object
	 */
	public function isACValidation($access_code){
		$get_array = array("AccessCode"=>$access_code);

		try{
			//initialize the class
			$this->gcurl = new gCurl($this->cfg['urls']['ac_validation'].http_build_query($get_array));

			//the content received
			$result = $this->gcurl->exec();
			$result->data = json_decode($result->body);
		}catch (gksException $E){
			//get err messages
			$message = $E->getTextMessage();
			$result->status_code = 0;
			toLog(UsersModel::getUserToLog().' '.$message.' SEND DATA:'.json_encode($get_array), 4, 'ac_validation', 'history_request');
		}

		if($result->status_code == 200){
			toLog(UsersModel::getUserToLog().' SEND DATA:'.json_encode($get_array). " REQUEST STATUS: 200 REQUEST DATA:".$result->body, 0, 'ac_validation', 'history_request');
		}elseif($result->status_code > 0){
			toLog(UsersModel::getUserToLog().' SEND DATA:'.json_encode($get_array). " REQUEST STATUS: ".$result->status_code." REQUEST DATA: ".$result->body, 1, 'ac_validation', 'history_request');
		}
		$this->gcurl = null;
		return $result;
	}


	/**
	 * sendACRedemption
	 * The function reports the redemption of eBook.
	 *
	 * <code>
	 *  $eC_API = new Econtent_API();
	 *  $result = $eC_API->sendACRedemption("123456", "email@test.com", "firstName", "LastName");
	 * </code>
	 *
	 * @param string $access_code
	 * @param string $email
	 * @param string $fname
	 * @param string $lname
	 * @version 2013-06-23
	 * @return object
	 */
	public function sendACRedemption($access_code, $email, $fname, $lname){
		$get_array = array(
				"AccessCode" => $access_code,
				"Email" => $email,
				"FirstName" => $fname,
				"LastName" => $lname
			);

		try{
			//initialize the class
			$this->gcurl = new gCurl($this->cfg['urls']['ac_redemption'].http_build_query($get_array));

			//the content received
			$result = $this->gcurl->exec();
		}catch (gksException $E){
			//get err messages
			$message = $E->getTextMessage();
			$result->status_code = 0;
			toLog(UsersModel::getUserToLog().' '.$message.' SEND DATA:'.json_encode($get_array), 4, 'ac_redemption', 'history_request');
		}

		if($result->status_code == 200){
			toLog(UsersModel::getUserToLog().' SEND DATA:'.json_encode($get_array). " REQUEST STATUS: 200 REQUEST DATA:".$result->body, 0, 'ac_redemption', 'history_request');
		}elseif($result->status_code > 0){
			toLog(UsersModel::getUserToLog().' SEND DATA:'.json_encode($get_array). " REQUEST STATUS: ".$result->status_code." REQUEST DATA:".$result->body, 2, 'ac_redemption', 'history_request');
		}
		$this->gcurl = null;
		return $result;
	}
}